/** 
* @author : 褚国强
* @date 创建时间：2021年11月17日 上午9:57:55 
* @version 1.0
*/
package com.bsoft.project.controller;

import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bsoft.project.service.RsfComlfServiceSvc;
import com.bsoft.project.utils.StringUtils;

/** 
* @author : 褚国强
* @date 创建时间：2021年11月17日 上午9:57:55 
* @Description: TODO
* @version 1.0 
* @parameter 
* @since 
* @return 
*/
@RestController
@RequestMapping("rsfComl")
public class RsfComlfsServiceController {
	
	private final Logger logger = LoggerFactory.getLogger(RsfComlfsServiceController.class);

	@Autowired
	private RsfComlfServiceSvc service;
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2021年11月17日
	 * @Description:  医保目录下载
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "rsfComIfsService", method = RequestMethod.POST)
	public HashMap<String, Object> rsfComIfsService(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			int jgid = 1;
			if(StringUtils.isNotEmpty(param.get("jgid"))){
				jgid = (int)param.get("jgid");
			}
			service.init(jgid);
			boolean excute = true;
			while(excute){
				HashMap<String, Object> downloadParm = service.rsfComIfsServiceLoop(param);
				String parStr = downloadParm.get("data").toString();
				
				if(("已经是最新数据").equals(parStr)){
					return result;
				}else{
					result = service.rsfComIfsService(parStr);					
					logger.info("========  type: " + result.get("type") + "======= fileQuryNo: " + result.get("fileQuryNo") + "===  解析完毕最大ver为:" + result.get("maxVerStr"));
					HashMap<String, Object> data = new HashMap<String, Object>();
					data.put("ver", result.get("maxVerStr"));
					HashMap<String, Object> input = new HashMap<String, Object>();
					input.put("data", data);
					param.put("input", input);
				}				
			}
//			String parStr = JSON.toJSONString(param);
//			JSONObject jsonObject = JSONObject.parseObject(parStr);
//			service.insertUpdateZFBL(jsonObject,"",1L);
//			result.put("data", service.insertUpdateZFBL(jsonObject));
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
	
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2021年12月27日
	 * @Description:  自付比例更新   医疗目录查询
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "sentJsonService", method = RequestMethod.POST)
	public HashMap<String, Object> sentJsonService(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			int jgid = 1;
			if(StringUtils.isNotEmpty(param.get("jgid"))){
				jgid = (int)param.get("jgid");
			}
			service.init(jgid);
			String parStr = JSON.toJSONString(param);
			JSONObject jsonObject = JSONObject.parseObject(parStr);
			service.sentJsonService(jsonObject,jsonObject.getJSONObject("input").getJSONObject("data").getString("updt_time"),1L);
//			result.put("data", service.insertUpdateZFBL(jsonObject));
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2021年12月27日
	 * @Description:  医疗目录查询
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "rsfMlcxService", method = RequestMethod.POST)
	public HashMap<String, Object> rsfMlcxService(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			int jgid = 1;
			if(StringUtils.isNotEmpty(param.get("jgid"))){
				jgid = (int)param.get("jgid");
			}
			service.init(jgid);
			String parStr = JSON.toJSONString(param);
			service.rsfMlcxService(parStr);
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
	
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2022年01月11日
	 * @Description:  医疗目录上传
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "rsfUpload", method = RequestMethod.POST)
	public HashMap<String, Object> rsfUpload(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			service.rsfUpload(param);
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2022年01月11日
	 * @Description:  自付比例上传
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "rsfZfblUpload", method = RequestMethod.POST)
	public HashMap<String, Object> rsfZfblUpload(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			service.rsfZfblUpload(param);
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
	
	
	/**
	 * @author : 褚国强
	 * @date 创建时间：2022年01月17日
	 * @Description:  国家医保对账 下载
	 * @version 1.0
	 * @parameter
	 * @since
	 * @return
	 */
	@RequestMapping(value = "rsfGjybDZ", method = RequestMethod.POST)
	public HashMap<String, Object> rsfGjybDZ(@RequestBody HashMap<String,Object> param) {
		HashMap<String, Object> result = new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "SUCCESS!");
		try {
			int jgid = 1;
			if(StringUtils.isNotEmpty(param.get("jgid"))){
				jgid = (int)param.get("jgid");
			}
			service.init(jgid);
			String parStr = JSON.toJSONString(param);
			result.put("data", service.rsfGjybDZ(parStr));
		} catch (Exception e) {
			result.put("code", 500);
			result.put("message", "error!" + e.getMessage());
			logger.error(e.getMessage(), e);
		}
		logger.info("---------------------" + JSON.toJSONString(result));
		return result;
	}
}
